<?php

declare(strict_types=1);

namespace app\user\model;

use sheep\model\user\User as BaseUser;

class User extends BaseUser
{

    public function setMobileAttr($value, $data)
    {
        if($value !== "") {
            return $value;
        }
        return null;
    }

    public function setUsernameAttr($value, $data)
    {
        if($value !== "") {
            return $value;
        }
        return null;
    }

    public function setEmailAttr($value, $data)
    {
        if($value !== "") {
            return $value;
        }
        return null;
    }

    public function setPasswordAttr($value, $data)
    {
        $salt = mt_rand(1000, 9999);
        $this->set('salt', $salt);
        return (new static)->encryptPassword($value, $salt);
    }

    public function getNicknameHideAttr($value, $data)
    {
        $value = $value ?: ($data['nickname'] ?? '');

        return $value ? string_hide($value, 2) : $value;
    }

    public function parentUser()
    {
        return $this->hasOne(User::class, 'id', 'parent_user_id')->field(['id', 'avatar', 'nickname', 'gender']);
    }
}
